package com.cxhome.cxinfox.export;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

import com.cxhome.cxinfox.order.Order;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.system.Department;
import com.traffic.managent.model.system.User;

/**
 * 导出管理
 * 
 * @author Guoxing
 *
 */
@ControllerBind(controllerKey = "/system/export")
public class EveryExportController extends CxBaseController {

	private static final String path = "/pages/system/customerinfo/customerinfo_";

	public void index() {
	}

	public void orderinfoForVasExport() {
		HttpServletResponse response = getResponse();
		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String outputFile = "Adm_export-" + time;
		response.setContentType("application/octet-stream ");
		response.setHeader("Content-Disposition", "attachment;filename=\"" + outputFile + ".zip" + "\"");
		Map<String, Object> paraMap = new HashMap<String, Object>();

		// 根据组织结构参数来查询权限数据
		String departmentId = getSessionUser().getStr("departmentId");
		String tempIdsStr = Department.dao.queryChildIds(departmentId);
		String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
		String idsStr = "";
		if (tempIdsStr1.contains(",")) {
			idsStr = "('" + tempIdsStr1 + "')";
		} else {// 组织机构只有一个时查询语句会报错
			idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
		}
		paraMap.put("departmentId", idsStr);

		paraMap.put("status", getPara("attr.status"));
		paraMap.put("customerName", getPara("attr.customerName"));
		paraMap.put("ids", getPara("attr.ids"));
		paraMap.put("orderNo", getPara("attr.orderNo"));
		paraMap.put("phoneNo", getPara("attr.phoneNo"));
		paraMap.put("operator", getPara("attr.operator"));
		paraMap.put("providerId", getPara("attr.providerId"));
		paraMap.put("startTime", getPara("startTime"));
		paraMap.put("endTime", getPara("endTime"));
		paraMap.put("payMode", getPara("attr.payMode"));
		Order order = Order.dao.orderCount(paraMap);
		PoiKitNew_Utils poi = new PoiKitNew_Utils();
		if (order != null) {// 有用户付款记录，展示支付相关信息
			poi.columns(new String[] { "transactionNo", "cstmOrderNo", "customerName", "ids", "orderNo", "payChannel",
					"sellPrice", "providerName", "vasProdName", "productPrice", "phoneNo", "status", "orderTime",
					"openTime", "message", "errorMsg" });
			poi.headers(new String[] { "支付宝交易号", "客户流水号", "客户名称", "(长)订单号", "(短)订单号", "支付渠道", "订单金额(元)", "供应商", "商品名称",
					"商品价格", "手机号码", "订单状态", "订购时间", "开通时间", "描述信息", "供应商侧描述信息" });
			poi.cellWidth(5000);
		} else {// 客户付款
			poi.columns(new String[] { "cstmOrderNo", "customerName", "ids", "orderNo", "sellPrice", "providerName",
					"vasProdName", "productPrice", "phoneNo", "status", "orderTime", "openTime", "message",
					"errorMsg" });
			poi.headers(new String[] { "客户流水号", "客户名称", "(长)订单号", "(短)订单号", "订单金额(元)", "供应商", "商品名称", "商品价格", "手机号码",
					"订单状态", "订购时间", "开通时间", "描述信息", "供应商侧描述信息" });
			poi.cellWidth(5000);
		}
		int i = 0;
		while (true) {
			paraMap.put("start", i * (PoiKitNew_Utils.MAX_ROWS));
			paraMap.put("end", PoiKitNew_Utils.MAX_ROWS);
			List<Order> orderList = Order.dao.orderList(paraMap);
			if (orderList != null && orderList.size() > 0) {
				poi.export(orderList, "sheet" + i);
				i++;
			} else {
				break;
			}
			orderList = null;
		}
		ZipOutputStream os = null;
		try {
			os = new ZipOutputStream(response.getOutputStream());
			ZipEntry entry = new ZipEntry(outputFile + ".xlsx");
			os.putNextEntry(entry);
			poi.getWb().write(os);
			os.flush();
			os.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		renderNull();
	}
	/**
	 * 客户端导出功能
	 */
	
	public void orderinfoForCusExport() {
		HttpServletResponse response = getResponse();
		String time = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		String outputFile = "Customer_export-" + time;
		response.setContentType("application/octet-stream ");
		response.setHeader("Content-Disposition", "attachment;filename=\"" + outputFile + ".zip" + "\"");
		Map<String, Object> paraMap = new HashMap<String, Object>();

		String customerId = getSessionUser().getStr("customerId");
		// 用户没有绑定账户时
		if (customerId != null && !"".equals(customerId)) { // 管理员用户登录时,没有账户信息,根据组织机构来查询数据
			// 数据权限
			String departmentId = getSessionUser().getStr("departmentId");
			// 之前的查询是直接根据组织机构来模糊查询的" and u.departmentId like #'$departmentId$%'#
			// ",
			// 但是实际上由于组织机构拖动改变, 模糊查询查询出来的数据会错乱掉.
			// ***从组织机构数据表中查询出该组织结构所有的实际子组织机构id(含本身)***
			String tempIdsStr = Department.dao.queryChildIds(departmentId);
			String tempIdsStr1 = tempIdsStr.replaceAll(",", "','");
			String idsStr = "";
			if (tempIdsStr1.contains(",")) {
				idsStr = "('" + tempIdsStr1 + "')";
			} else {// 组织机构只有一个时查询语句会报错
				idsStr = "('" + tempIdsStr1 + "','" + tempIdsStr1 + "')";
			}
			// 加入组织结构参数
			paraMap.put("departmentId", idsStr);
			paraMap.put("customerId", "");
		} else {
			paraMap.put("departmentId", "");
			paraMap.put("customerId", customerId);
		}

		paraMap.put("status", getPara("attr.status"));
		paraMap.put("customerName", getPara("attr.customerName"));
		paraMap.put("ids", getPara("attr.ids"));
		paraMap.put("orderNo", getPara("attr.orderNo"));
		paraMap.put("phoneNo", getPara("attr.phoneNo"));
		paraMap.put("operator", getPara("attr.operator"));
		paraMap.put("startTime", getPara("startTime"));
		paraMap.put("endTime", getPara("endTime"));
		paraMap.put("filterAuthOrder", "1");
		PoiKitNew_Utils poi = new PoiKitNew_Utils();
		int i = 0;
		while (true) {
			paraMap.put("start", i * (PoiKitNew_Utils.MAX_ROWS));
			paraMap.put("end", PoiKitNew_Utils.MAX_ROWS);
			List<Order> orderList = Order.dao.orderList(paraMap);
			if (orderList != null && orderList.size() > 0 && "0".equals(orderList.get(0).get("payMode"))) {
				// 客户付款
				if (i == 0) {
					poi.columns(new String[] { "cstmOrderNo", "customerName", "ids", "orderNo", "sellPrice",
							"vasProdName", "productPrice", "phoneNo", "status", "orderTime", "openTime", "message",
							"currentAmount" });
					poi.headers(new String[] { "客户流水号", "客户名称", "(长)订单号", "(短)订单号", "订单金额(元)", "商品名称", "商品价格", "手机号码",
							"订单状态", "订购时间", "开通时间", "描述信息", "当前余额" }).cellWidth(5000);
					;
				}
			} else {// 用户付款
				if (i == 0) {
					poi.columns(new String[] { "transactionNo", "cstmOrderNo", "customerName", "ids", "orderNo",
							"payChannel", "sellPrice", "vasProdName", "productPrice", "phoneNo", "status", "orderTime",
							"openTime", "message", "currentAmount" });
					poi.headers(new String[] { "支付宝交易号", "客户流水号", "客户名称", "(长)订单号", "(短)订单号", "支付渠道", "订单金额(元)", "商品名称",
							"商品价格", "手机号码", "订单状态", "订购时间", "开通时间", "描述信息", "当前余额" }).cellWidth(5000);
				}
			}

			if (orderList != null && orderList.size() > 0) {
				poi.export(orderList, "sheet" + i);
				i++;
			} else {
				break;
			}
			orderList = null;
		}
		ZipOutputStream os = null;
		try {
			os = new ZipOutputStream(response.getOutputStream());
			ZipEntry entry = new ZipEntry(outputFile + ".xlsx");
			os.putNextEntry(entry);
			poi.getWb().write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		renderNull();
	}

}
